<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <!-- <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item> -->
      <!-- <el-form-item label="状态 1正常 0冻结" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态 1正常 0冻结"></el-input>
    </el-form-item> -->
      <el-form-item label="标签" prop="tdk">
        <el-input
          v-model="dataForm.tdk"
          :disabled="true"
          placeholder="标签"
        ></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="watermark">
        <div id="editor"></div>
      </el-form-item>
      <!-- <el-form-item label="友情链接" prop="links">
        <el-input v-model="dataForm.links" placeholder="友情链接"></el-input>
      </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import Editor from "wangeditor";
export default {
  data() {
    return {
      uploadUrledit: this.$http.adornUrl(
        `/testing/file/uploadedit?token=${this.$cookie.get("token")}`
      ),
      visible: false,
      dataForm: {
        id: 0,
        createTime: "",
        updateTime: "",
        status: "",
        tdk: "",
        watermark: "",
        links: "",
      },
      dataRule: {
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
        updateTime: [
          { required: true, message: "修改时间不能为空", trigger: "blur" },
        ],
        status: [
          {
            required: true,
            message: "状态 1正常 0冻结不能为空",
            trigger: "blur",
          },
        ],
        tdk: [{ required: true, message: "标签不能为空", trigger: "blur" }],
        watermark: [
          { required: true, message: "内容不能为空", trigger: "blur" },
        ],
        links: [{ required: true, message: "内容不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
  beforeDestroy() {
      // 销毁编辑器
      if (this.editor != null) {
        this.editor.destroy();
        this.editor = null;
      }
    },
    getHtml() {
      return this.editor.txt.html();
    },
    initedit() {
      this.editor = new Editor("#editor");
      // 配置菜单
      // 限制类型
      // editor.config.uploadImgAccept = ['jpg', 'jpeg', 'png', 'gif', 'bmp']
      this.editor.config.uploadImgMaxLength = 1; // 一次最多上传 1 个图片
      this.editor.config.showLinkImg = false; //关闭网络路径图片方式
      this.editor.config.uploadFileName = "file"; // formdata中的name属性
      // this.editor.config.uploadImgShowBase64 = true;
      this.editor.config.uploadImgServer = this.uploadUrledit;
      // 配置菜单栏，删减菜单，调整顺序
      this.editor.config.menus = [
        "undo", // 撤销
        "redo", // 重复
        "head", // 标题
        "bold", // 粗体
        "fontSize", // 字号
        "fontName", // 字体
        "italic", // 斜体
        "foreColor", // 文字颜色
        "backColor", // 背景颜色
        "justify", // 对齐方式
        "image", // 插入图片
      ];
      this.editor.create();
    },
    init(id) {
 
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
             this.beforeDestroy();
      this.initedit();
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/testing/platformsettings/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.createTime = data.platformSettings.createTime;
              this.dataForm.updateTime = data.platformSettings.updateTime;
              this.dataForm.status = data.platformSettings.status;
              this.dataForm.tdk = data.platformSettings.tdk;
              this.dataForm.watermark = data.platformSettings.watermark;
              this.dataForm.links = data.platformSettings.links;
              this.editor.txt.html(this.dataForm.watermark); // 重新设置编辑器内容
              this.dataForm.watermark = data.platformSettings.watermark;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.dataForm.watermark = this.getHtml();
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/testing/platformsettings/${
                !this.dataForm.id ? "save" : "update"
              }`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              createTime: this.dataForm.createTime,
              updateTime: this.dataForm.updateTime,
              status: this.dataForm.status,
              tdk: this.dataForm.tdk,
              watermark: this.dataForm.watermark,
              links: this.dataForm.links,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
